package com.ruoyi.common.h1wUtils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.chatroom.ChatRoomListResult;
import cn.jmessage.api.chatroom.CreateChatRoomResult;
import cn.jmessage.api.common.model.RegisterInfo;
import cn.jmessage.api.common.model.UserPayload;
import cn.jmessage.api.common.model.chatroom.ChatRoomPayload;
import cn.jmessage.api.common.model.message.MessageBody;
import cn.jmessage.api.message.MessageListResult;
import cn.jmessage.api.message.SendMessageResult;
import cn.jmessage.api.user.UserInfoResult;
import cn.jmessage.api.user.UserListResult;
import org.apache.commons.lang3.StringUtils;

public class IMUtils {
    private final static String appkey = "5afd07a1c570294c76d96860";
    private final static String masterSecret = "c6e0cb50fa8259d07c425740";
    private final static JMessageClient client = new JMessageClient(appkey, masterSecret);

    /**
     * 用户注册
     */
    public static void registerUsers(String userName, String nickName, String avatar) throws APIConnectionException, APIRequestException {
        RegisterInfo registerInfo = RegisterInfo.newBuilder()
                .setUsername(userName).setNickname(nickName)
                .setPassword(userName)
                .setAvatar(StringUtils.isNotBlank(avatar) ? avatar : "https://img.bajiaostar.com/FsaUOBWsqTLpiKPjbLzJxQV5hkKG")
                .build();
        RegisterInfo[] registerInfos = new RegisterInfo[1];
        registerInfos[0] = registerInfo;

        String string = client.registerUsers(registerInfos);

        System.out.println(string);
    }

    /**
     * 用户更新头像
     */
    public static void updateUsersAvatar(String userName, String avatar) throws APIConnectionException, APIRequestException {
        UserPayload payload = UserPayload.newBuilder().setAvatar(avatar).build();
        client.updateUserInfo(userName, payload);
    }

    /**
     * 用户更新昵称
     */
    public static void updateUsersNickName(String userName, String nickName) throws APIConnectionException, APIRequestException {
        UserPayload payload = UserPayload.newBuilder().setNickname(nickName).build();
        client.updateUserInfo(userName, payload);
    }

    /**
     * 用户禁用
     */
    public static void forbidUser(String username, boolean disable) throws APIConnectionException, APIRequestException {
        client.forbidUser(username, disable);
    }

    /**
     * 用户删除
     */
    public static void deleteUser(String username) throws APIConnectionException, APIRequestException {
        client.deleteUser(username);
    }

    /**
     * 创建聊天室
     */
    public static String createChatRoom(String username, String roomName) throws APIConnectionException, APIRequestException {
        CreateChatRoomResult chatRoom = client.createChatRoom(ChatRoomPayload.newBuilder()
                .setOwnerUsername(username)
                .setName(roomName)
                .build());
        return chatRoom.getChatroom_id().toString();
    }

    /**
     * 获取聊天室详情
     */
    public static ChatRoomListResult getChatroomInfo(String imId) throws APIConnectionException, APIRequestException {
        ChatRoomListResult result = client.getBatchChatRoomInfo(Long.parseLong(imId));
        return result;
    }


    /**
     * 移除聊天室
     */
    public static void removeChatRoomMembers(long roomId, String members) throws APIConnectionException, APIRequestException {
        client.removeChatRoomMembers(roomId, members);
    }


    /**
     * 获取用户列表
     */
    public static UserListResult getUsers() throws APIConnectionException, APIRequestException {
        return client.getUserList(0, 30);
    }

    /**
     * 获取好友列表
     *
     * @throws APIConnectionException
     * @throws APIRequestException
     */
    public static UserInfoResult[] getFriends(String userName) throws APIConnectionException, APIRequestException {
        return client.getFriendsInfo(userName);
    }

    /**
     * 添加好友
     */
    public static ResponseWrapper addFriends(String userName, String... users) throws APIConnectionException, APIRequestException {
        return client.addFriends(userName, users);
    }
    /**
     * 删除好友
     */
    public static ResponseWrapper delFriends(String userName,String... users) throws APIConnectionException, APIRequestException {
        return client.deleteFriends(userName,users);
    }

    /**
     * 获取用户信息
     * @param userName
     * @throws APIConnectionException
     * @throws APIRequestException
     */
    public static void getUserInfo(String userName) throws APIConnectionException, APIRequestException {
        UserInfoResult res = client.getUserInfo(userName);
    }

    /**
     * 修改密码
     * @param userName
     * @param password
     * @throws APIConnectionException
     * @throws APIRequestException
     */
    public static void updatePassword(String userName,String password) throws APIConnectionException, APIRequestException {
        client.updateUserPassword(userName, password);
    }


    public static void main(String[] args) {
        try {
            IMUtils.getUserInfo("u103");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
